<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        {{username}} {{age}} {{user.name}}
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
    <script>
        const vue = new Vue({
            el: '#app',
            data: {
                username: '',
                age: 0,
                user: {
                    name: 'dcj',
                    password: null
                },
                // 不用直接声明,直接挂载就可以了
                // timerId: null
            },
            watch: {
                username(newValue, oldValue) {
                    console.log('数据发生变化', newValue, oldValue);
                },

                'user.name'(newValue) {
                    console.log('嵌套属性发生变化', newValue);
                },

                // 防抖
                'user.name'(newValue) {
                    clearTimeout(this.timerId);
                    this.timerId = setTimeout(async () => {
                        const res = await axios({

                        })
                    }, 1000)
                    console.log('嵌套属性发生变化', newValue);
                },


                // 所有属性都监视
                user: {
                    // 所有属性都监视
                    deep: true,
                    // 初始化立即执行一次handler()
                    immediate: true,
                    handler(newValue) {
                        clearTimeout(this.timerId);
                        this.timerId = setTimeout(async () => {
                            const res = await axios({

                            })
                        }, 1000)
                    }
                }
            }
        })
    </script>
</body>

</html>